﻿/**
 * SAN ANTONIO HEALTH CENTER PATIENT INFORMATION MANAGEMENT SYSTEM
 * ===============================================================
 * Title: VACCINE MODULE
 * Author: MONICA DM. TOSINO
 * Description: Add vaccination information about the patient
 * ===============================================================
 * **/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.OleDb;

namespace sahcpims
{
    public partial class vaccine : Form
    {
        /** GLOBAL VARIABLES :: being accessed by other forms :: **/
        public int familyNo;
        public string fname;
        public string mName;
        public string sname;
        public string bday;
        public string weight;
        public string checkuptype;

        public int empNumber;
        public string empName;

        public string tox = null;
        public string bcg = null;
        public string nbs = null;
        public string dpt = null;
        public string hepa = null;

        public int doFlag = 0;

        public vaccine()
        {
            InitializeComponent();
        }
        
       
        public void displayInformation()
        {
            familyInformationLabel.Text = fname + " " + mName + " " + sname;
            familyNumberMismo.Text = familyNo.ToString();
            bDateInfo.Text = bday;
            weightInfo.Text = weight;
            checkupTypeInfo.Text = checkuptype;

            empNumLabel.Text = empNumber.ToString();
            employeeLabel.Text = empName;
        }

        private void vaccine_Load(object sender, EventArgs e)
        {
            displayInformation();
            this.chekupdate.Text = DateTime.Now.ToShortDateString();
        }

        private void logOutButton_Click(object sender, EventArgs e)
        {
            logIn logout = new logIn();
            logout.Visible = true;
            this.Visible = false;
        }

        private void addVaccineButton_Click(object sender, EventArgs e)
        {
            /** Checks first if the forms are properly filled-up **/
            if (this.motherTextBox.Text == "" ||
              (this.nbsTextBox.Text == "" && !this.noNBS.Checked) ||
              (!this.BreastFeed.Checked && !this.Bottle.Checked && !this.nfcheckBox.Checked) ||
              (!this.DPT1.Checked && !this.Hepa1.Checked && !this.bcgCheckbox.Checked && !this.toxoidCheckBox.Checked))
            {
                //if atleast one is empty, prompt the user
                this.memberVerificationLabel.Text = "There are still some fields empty.";
            }
            /** Checks if the corresponding "duration" fields have input **/
            else if ((this.BreastFeed.Checked && this.breastfedDur.Text == "") || (this.Bottle.Checked && this.bottlefedDur.Text == ""))
            {
                //prompt the user
                this.memberVerificationLabel.Text = "Please enter feeding duration.";
            }
            /** If the forms are properly filled-up **/
            else
            {
                this.memberVerificationLabel.Text = "."; //clears the prompt message
                //serve as flags
                if (this.nbsTextBox.Text != "" && !this.noNBS.Checked) { nbs = "Yes"; }
                if (this.bcgCheckbox.Checked) { bcg = "Yes"; }
                if (this.toxoidCheckBox.Checked) { tox = "Yes"; }
                if (this.DPT1.Checked) { dpt = "Yes"; }
                if (this.Hepa1.Checked) { hepa = "Yes"; }

                //Connection String
                string conString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=..\\SAHCPIMS_database.mdb";
                OleDbConnection conn = new OleDbConnection(conString);
                DataSet ds = new DataSet();
                //opens the connection to the database
                conn.Open();
                //try to add entry in the Vaccine table
                try
                {
                    OleDbDataAdapter tagalagay = new OleDbDataAdapter("INSERT into Vaccine "
                                              + "values('" + familyNo + "','" + fname + "','" + mName + "','" + sname + "','" + DateTime.Now.ToShortDateString() + "','" + motherTextBox.Text + "')", conn);
                    tagalagay.Fill(ds);
                    doFlag = 0;

                }
                //catches the exception if the insertion failed due to data redundancy
                catch (OleDbException)
                {
                    memberVerificationLabel.Text = "Data not Saved in Database. Vaccine Information Already Exist.";
                    doFlag = 1;
                }

                //if the member underwent a New Born Screening Test
                if (nbs == "Yes")
                {   //tries to insert the date of NBS Test in the NewBornScreening table
                    try
                    {
                        OleDbDataAdapter tagalagay = new OleDbDataAdapter("INSERT into NewBornScreening "
                                          + "values('" + familyNo + "','" + fname + "','" + mName + "','" + sname + "','" + this.nbsTextBox.Text + "')", conn);

                        tagalagay.Fill(ds);
                        doFlag = 0;
                    }
                    //catches the exception for incorrect format
                    catch (FormatException)
                    {
                        memberVerificationLabel.Text = "Invalid Input in the New Born Screening field. Try Again.";
                        doFlag = 1;

                    }
                    //catches the exception if the insertion failed due to data redundancy
                    catch (OleDbException)
                    {
                        memberVerificationLabel.Text = "Data not Saved in Database." + "\n" + "New Born Screening Data Already Exist.";
                        doFlag = 1;
                    }

                }

                /**======= Feeding Information =======**/
                if (this.BreastFeed.Checked)
                {
                    try
                    {
                        OleDbDataAdapter tagalagay = new OleDbDataAdapter("INSERT into Feeding "
                            + "values('" + familyNo + "','" + fname + "','" + mName + "','" + sname + "','" + "BreastFed" + "','" + this.breastfedDur.Text + "')", conn);

                        tagalagay.Fill(ds);
                        doFlag = 0;
                    }
                    catch (OleDbException)
                    {
                        memberVerificationLabel.Text = "Data not Saved in Database." + "\n" + "Feeding Data Already Exist.";
                        doFlag = 1;
                    }
                }
                if (this.Bottle.Checked)
                {
                    try
                    {
                        OleDbDataAdapter tagalagay = new OleDbDataAdapter("INSERT into Feeding "
                            + "values('" + familyNo + "','" + fname + "','" + mName + "','" + sname + "','" + "BottleFed" + "','" + this.bottlefedDur.Text + "')", conn);

                        tagalagay.Fill(ds);
                        doFlag = 0;
                    }
                    catch (OleDbException)
                    {
                        memberVerificationLabel.Text = "Data not Saved in Database." + "\n" + "Feeding Data Already Exist.";
                        doFlag = 1;
                    }
                }
                if (this.nfcheckBox.Checked)
                {
                    try
                    {
                        OleDbDataAdapter tagalagay = new OleDbDataAdapter("INSERT into Feeding "
                            + "values('" + familyNo + "','" + fname + "','" + mName + "','" + sname + "','" + "NotFed, 0" + "')", conn);

                        tagalagay.Fill(ds);
                        doFlag = 0;
                    }
                    catch (OleDbException)
                    {
                        memberVerificationLabel.Text = "Data not Saved in Database." + "\n" + "Feeding Data Already Exist.";
                        doFlag = 1;
                    }
                }

                /** ========= Vaccines ========= **/
                if (bcg == "Yes")
                {
                    try
                    {
                        OleDbDataAdapter tagalagay = new OleDbDataAdapter("INSERT into BCG "
                                                      + "values('" + familyNo + "','" + fname + "','" + mName + "','" + sname + "','" + DateTime.Now.ToShortDateString() + "')", conn);
                        tagalagay.Fill(ds);
                        doFlag = 0;
                    }
                    catch (OleDbException)
                    {
                        memberVerificationLabel.Text = "Data not Saved in Database. BCG Data Already Exist.";
                        doFlag = 1;
                    }

                }
                if (tox == "Yes")
                {
                    try
                    {
                        OleDbDataAdapter tagalagay = new OleDbDataAdapter("INSERT into Toxoid "
                                                      + "values('" + familyNo + "','" + fname + "','" + mName + "','" + sname + "','" + DateTime.Now.ToShortDateString() + "')", conn);
                        tagalagay.Fill(ds);
                        doFlag = 0;
                    }
                    catch (OleDbException)
                    {
                        memberVerificationLabel.Text = "Data not Saved in Database. Toxoid Data Already Exist.";
                        doFlag = 1;
                    }

                }
                if (dpt == "Yes")
                {
                    try
                    {
                        OleDbDataAdapter tagalagay = new OleDbDataAdapter("INSERT into DPT "
                                                      + "values('" + familyNo + "','" + fname + "','" + mName + "','" + sname + "','" + DateTime.Now.ToShortDateString() + "')", conn);
                        tagalagay.Fill(ds);
                        doFlag = 0;
                    }
                    catch (OleDbException)
                    {
                        memberVerificationLabel.Text = "Data not Saved in Database. DPT Data Already Exist.";
                        doFlag = 1;
                    }
                }
                if (hepa == "Yes")
                {
                    try
                    {
                        OleDbDataAdapter tagalagay = new OleDbDataAdapter("INSERT into Hepa "
                                                      + "values('" + familyNo + "','" + fname + "','" + mName + "','" + sname + "','" + DateTime.Now.ToShortDateString() + "')", conn);
                        tagalagay.Fill(ds);
                        doFlag = 0;
                    }
                    catch (OleDbException)
                    {
                        memberVerificationLabel.Text = "Data not Saved in Database. Hepatitis Data Already Exist.";
                        doFlag = 1;
                    }
                }
                //closes database
                conn.Close();
                this.vaccinepanel.Visible = false;
                this.success.Visible = true;
            }
        }

        private void BreastFeed_CheckedChanged(object sender, EventArgs e)
        {
            if (this.BreastFeed.Checked)
            {
                this.breastfedDur.Enabled = true;
                this.nfcheckBox.Enabled = false;
            }
            if (!this.BreastFeed.Checked)
            {
                this.breastfedDur.Enabled = false;
                this.breastfedDur.Text = "";
                if (!this.Bottle.Checked)
                {
                    this.nfcheckBox.Enabled = true;
                }
            }
        }

        private void Bottle_CheckedChanged(object sender, EventArgs e)
        {
            if (this.Bottle.Checked)
            {
                this.bottlefedDur.Enabled = true;
                this.nfcheckBox.Enabled = false;
            }
            if (!this.Bottle.Checked)
            {
                this.bottlefedDur.Enabled = false;
                this.bottlefedDur.Text = "";
                if (!this.BreastFeed.Checked)
                {
                    this.nfcheckBox.Enabled = true;
                }
            }
        }

        private void nfcheckBox_CheckedChanged(object sender, EventArgs e)
        {
            if (this.nfcheckBox.Checked)
            {
                this.BreastFeed.Enabled = false;
                this.BreastFeed.Checked = false;
                this.breastfedDur.Enabled = false;
                this.breastfedDur.Text = "";

                this.Bottle.Enabled = false;
                this.Bottle.Checked = false;
                this.bottlefedDur.Enabled = false;
                this.bottlefedDur.Text = "";
            }
            if (!this.nfcheckBox.Checked)
            {
                this.BreastFeed.Enabled = true;
                this.breastfedDur.Enabled = true;

                this.Bottle.Enabled = true;
                this.bottlefedDur.Enabled = true;
            }
        }

        private void noNBS_CheckedChanged(object sender, EventArgs e)
        {
            if (this.noNBS.Checked)
            {
                this.nbsTextBox.Enabled = false;
                this.nbsTextBox.Text = "";
            } if (!this.noNBS.Checked)
            {
                this.nbsTextBox.Enabled = true;
            }
        }

        
        
    }
}
 